#include<iostream>
#include<vector>
using namespace std;
class Solution {
public:
    vector<int> dailyTemperatures(vector<int>& temperatures) {
        int n=temperatures.size();
        vector<int>ans(n);
        vector<int>stk;
        for(int i=0;i<n;i++)
        {
            while(!stk.empty()&&temperatures[stk.back()]<temperatures[i])
            {
                ans[stk.back()]=i-stk.back();
                stk.pop_back();
            }
            stk.push_back(i);
            
        }
        if(!stk.empty())
            {
                ans[stk.back()]=0;
                stk.pop_back();
            }
        return ans;
    }
};